package com.shanghaionstar.user.presenter;

import android.content.Context;

import com.onstar.cn.sqlite.entities.BroadcastHeaderEntity;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.shanghaionstar.user.iview.IBroadcastView;
import com.shanghaionstar.user.service.BroadcastService;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import rx.Subscription;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by Yonglang Hong on 2016/8/9.
 */
public class BroadcastPresenter extends BasePresenter {

    private IBroadcastView broadcastView;

    private BroadcastService broadcastService;

    private Subscription dbSubscription;

    public BroadcastPresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        broadcastService = new BroadcastService(context);
    }

    public void setBroadcastView(IBroadcastView broadcastView) {
        this.broadcastView = broadcastView;
    }

    public void loadMsgs() {
        subscription = broadcastService.loadMsgList()
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String result) {
                        if (broadcastView != null) {
                            broadcastView.onLoadMsgComplete();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (broadcastView != null && throwable != null) {
                            throwable.printStackTrace();
                            broadcastView.onLoadFromServiceErr(throwable);
                            broadcastView.onLoadMsgComplete();
                        }
                    }
                });
    }

    public void getAllListFromDB() {
        dbSubscription = broadcastService.getAllListFromDB()
                .map(new Func1<List<BroadcastHeaderEntity>, List<BroadcastHeaderEntity>>() {
                    @Override
                    public List<BroadcastHeaderEntity> call(List<BroadcastHeaderEntity> list) {
                        Collections.sort(list, new Comparator<BroadcastHeaderEntity>() {
                            @Override
                            public int compare(BroadcastHeaderEntity lhs, BroadcastHeaderEntity rhs) {
                                int result = 1;
                                try {
                                    result = compareDate(lhs.getPublishTime(), rhs.getPublishTime());
                                } catch (Exception ex) {
                                }
                                return result;
                            }
                        });
                        return list;
                    }
                })
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<List<BroadcastHeaderEntity>>() {
                    @Override
                    public void call(List<BroadcastHeaderEntity> result) {
                        if (broadcastView != null) {
                            if (result != null && !result.isEmpty())
                                broadcastView.showMsgList(result);
                            else
                                broadcastView.showNoMsg();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (broadcastView != null && throwable != null) {
                            throwable.printStackTrace();
                            broadcastView.onLoadFromDBErr(throwable);
                        }
                    }
                });
    }

    private int compareDate(String date1, String date2) throws Exception {
        //设定时间的模板
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //得到指定模范的时间
        Date d1 = sdf.parse(date1);
        Date d2 = sdf.parse(date2);
//        log.d("date1.."+date1+",..gettime().."+d1.getTime()+",date2.."+date2+",..gettime2().."+d2.getTime());
        //比较
        if (d1.getTime() - d2.getTime() > 0) {
            return -1;
        } else if (d1.getTime() - d2.getTime() == 0) {
            return 0;
        } else {
            return 1;
        }
    }


    public void update(BroadcastHeaderEntity headerEntity) {
        broadcastService.update(headerEntity);
    }

    @Override
    public void finish() {
        super.finish();
        if (dbSubscription != null) {
            dbSubscription.unsubscribe();
            dbSubscription = null;
        }
    }
}
